var common = common || {};
$(function() {

	common.serializeObject = function(form) {
		var o = {};
		$.each(form.serializeArray(), function(index) {
			if (this['value'] != undefined && this['value'].length > 0) {// 如果表单项的值非空，才进行序列化操作
				var value = this['value'];
				if (o[this['name']]) {
					o[this['name']] = o[this['name']] + "," + value;
				} else {
					o[this['name']] = value;
				}
			}
		});
		return o;
	};
	
	common.serializeRequestBody = function(form) {
		return JSON.stringify(common.serializeObject(form));
	};

	common.date2str = function(date, format) {
		var z = {
			y : date.getFullYear(),
			M : date.getMonth() + 1,
			d : date.getDate(),
			h : date.getHours(),
			m : date.getMinutes(),
			s : date.getSeconds()
		};
		return format.replace(/(y+|M+|d+|h+|m+|s+)/g, function(v) {
			return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1)))
					.slice(-(v.length > 2 ? v.length : 2));
		});
	};
	
	common.resetForm = function(form) {
		$(form).find("*[name]").each(
			function(i, element) {
				$("span[for='" + ((this.id && this.id != '') ? this.id : this.name) + "']").remove();
			});
	}
	
	common.setFormData = function(form,data){
		for(var p in data){    
			   debugger;
		       if(typeof(data[p])=="function"){       
		    	   data[p]();
		       }else{      
		           var input=$("#"+form).find("input[name='"+p+"']").eq(0);
		           if(input!=undefined){
		        	   input.val(data[p]);
		           }
		       }        
		}    
	}
	
	common.GetArgsFromHref = function(href, sArgName) {
	      var sHref = href;
	      var args = sHref.split("?");
	      var retval = "";
	      if (args[0] == sHref) {
	            return retval;
	      }
	      var str = args[1];
	      args = str.split("&");
	      for (var i = 0; i < args.length; i++) {
	            str = args[i];
	            var arg = str.split("=");
	            if (arg.length <= 1) continue;
	            if (arg[0] == sArgName) retval = arg[1];
	      }
	      return retval;
	}
});